package com.villaday.idf.activity;


import android.graphics.Bitmap;
import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.R;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.custom.FillInvitateCodeDialog;
import com.villaday.idf.model.equityEntity;
import com.villaday.idf.util.Constant;
import com.villaday.idf.util.LogeUtil;
import com.villaday.idf.util.OkHttpUtil;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.ToolBarUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static com.bumptech.glide.load.DecodeFormat.PREFER_ARGB_8888;

/**
 * 超级会员
 */
public class SvipEquityActivity extends BaseActivity implements OkHttpUtil.OnDataListener {


    @BindView(R.id.toolbar_text)
    public Toolbar toolbar;

    private FillInvitateCodeDialog fillInvitateCodeDialog;
    @BindView(R.id.sdv_bg)
    ImageView sdvBg;
    private Gson gson = new GsonBuilder().create();


    @Override
    protected int getViewResId() {
        return R.layout.activity_svip_equity;
    }

    @Override
    protected void init() {
        ToolBarUtil.initToolBar(toolbar, getIntent().getIntExtra("title", 0) == 0 ? "VIP权益" : "SVIP权益", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void loadData() {
        Map<String, String> map = new HashMap<>();
        OkHttpUtil.getJSON(this, Constant.URL.equity, map, this);
    }

    @OnClick({})
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }


    @Override
    public void onResponse(String url, String json) {
        if (!TextUtils.isEmpty(json)) {
            LogeUtil.e("equity", json);
            equityEntity fe = gson.fromJson(json, equityEntity.class);
            if (Constant.Strings.SUC.equals(fe.getCode())) {
                RequestOptions options = new RequestOptions()
                        .placeholder(R.drawable.ic_replace)
                        .error(R.drawable.ic_replace)
                        .priority(Priority.HIGH)
                        .format(PREFER_ARGB_8888)
                        .centerCrop();
                Glide.with(this)
                        .asBitmap()//强制Glide返回一个Bitmap对象
                        .load(fe.getBody().getImageUrl())
                        .apply(options)
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {

                                int width = resource.getWidth();
                                int height = resource.getHeight();
                                sdvBg.getLayoutParams().width = PhoneUtil.getPhoneWidth(SvipEquityActivity.this);
                                sdvBg.getLayoutParams().height = PhoneUtil.getPhoneWidth(SvipEquityActivity.this) * height / width;
                                sdvBg.requestLayout();
                                sdvBg.setImageBitmap(resource);
                            }


                        });
            }
        }
    }

    @Override
    public void onFailure(String url, String error) {

    }
}
